/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Bean;

import Action.CustomerSessionBeanLocal;
import Entity.Customer;
import java.util.Iterator;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.naming.InitialContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Cyrax
 */
@ManagedBean
@SessionScoped
public class CustomerBean {

    @EJB
    private CustomerSessionBeanLocal customerSessionBean;
    private Customer cu;
    private List<Customer> listCu;

    /** Creates a new instance of CustomerBean */
    public CustomerBean() {
        cu = new Customer();
    }

    /**
     * @return the cu
     */
    public Customer getCu() {
        return cu;
    }

    /**
     * @param cu the cu to set
     */
    public void setCu(Customer cu) {
        this.cu = cu;
    }

    public void register() {
        try {


            customerSessionBean.insertCustomer(cu);
            ExternalContext extcontext = FacesContext.getCurrentInstance().getExternalContext();
            FacesContext face = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) face.getExternalContext().getSession(true);
            session.setAttribute("userEmail", cu.getEmail());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public List<Customer> getListCustomer() {
        listCu = customerSessionBean.listAllCustomer();
        return listCu;
    }

    public void deleteCustomer(Customer cu) {
        try {
            customerSessionBean.deleteCustomer(cu);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void updateCustomer() {

        String email = cu.getEmail();
        customerSessionBean.updateCustomer(email, cu.getPassword(), cu.getFullName(), cu.getAddress(), cu.getPhone(), cu.getAge());

    }
    private String msEmail;

    public void checkCustomerEmail() {
        if (customerSessionBean.checkCustomerEmail(cu.getEmail())) {
            msEmail = "This Email is already existed";
        } else {
            msEmail = "";

        }
    }

    /**
     * @return the msEmail
     */
    public String getMsEmail() {
        return msEmail;
    }

    /**
     * @param msEmail the msEmail to set
     */
    public void setMsEmail(String msEmail) {
        this.msEmail = msEmail;
    }
    static InitialContext cnt = null;
    private String msg3;

    public String loginUser() {
        ExternalContext extcontext = FacesContext.getCurrentInstance().getExternalContext();
        try {

            cnt = new InitialContext();

            FacesContext face = FacesContext.getCurrentInstance();
            Iterator stIterator = customerSessionBean.listAllCustomer().iterator();
            while (stIterator.hasNext()) {
                Customer cu = (Customer) stIterator.next();

                if (cu.getEmail().equals(getCu().getEmail()) && cu.getPassword().equals(getCu().getPassword())) {
                    HttpSession session = (HttpSession) face.getExternalContext().getSession(true);
                    session.setAttribute("userEmai", cu.getEmail());
                    session.setAttribute("name", cu.getFullName());
                    session.setAttribute("customer", cu);

                    extcontext.redirect("Home.xhtml");
                    System.out.println("==========================Test===" + cu.getEmail());

                }



            }
            setMsg3("Enter Email or password fail");
            return "LoginUser";


        } catch (Exception ex) {
            System.out.println("==========================Loi2===");
            return "false";
        }
    }

    public String logoutUser() {
        FacesContext context = FacesContext.getCurrentInstance();
        HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
        if (session.getAttribute("userEmail") == null) {

            return "LoginUser";
        } else {
            session.invalidate();
            return "fasle";
        }
    }

    public void logout() {
        ExternalContext extcontext = FacesContext.getCurrentInstance().getExternalContext();

        try {
            FacesContext context = FacesContext.getCurrentInstance();
            HttpSession session = (HttpSession) context.getExternalContext().getSession(true);
            if (session.getAttribute("userEmail") == null) {

                extcontext.redirect("LoginUser.xhtml");
            } else {
                session.invalidate();
                extcontext.redirect("LoginUser.xhtml");
            }

        } catch (Exception e) {
        }
    }

    public String getMsg3() {
        return msg3;
    }

    /**
     * @param msg3 the msg3 to set
     */
    public void setMsg3(String msg3) {
        this.msg3 = msg3;
    }
}
